import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import ProductList from '@/views/ProductList.vue'
import Cart from '@/views/Cart.vue'
import ProductDetail from '@/views/ProductDetail.vue'
import OrderList from '@/views/OrderList.vue'
import UserCenter from '@/views/UserCenter.vue'
import MessageBoard from '@/views/MessageBoard.vue'
import AdminLayout from '@/views/admin/Layout.vue'
import AdminDashboard from '@/views/admin/Dashboard.vue'
import AdminUsers from '@/views/admin/Users.vue'
import AdminProducts from '@/views/admin/Products.vue'
import AdminOrders from '@/views/admin/Orders.vue'
import AdminMessages from '@/views/admin/Messages.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { guest: true }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { guest: true }
  },
  {
    path: '/products',
    name: 'ProductList',
    component: ProductList
  },
  {
    path: '/cart',
    name: 'Cart',
    component: Cart,
    meta: { requiresAuth: true }
  },
  {
    path: '/products/:id',
    name: 'ProductDetail',
    component: ProductDetail
  },
  {
    path: '/orders',
    name: 'OrderList',
    component: OrderList,
    meta: { requiresAuth: true }
  },
  {
    path: '/user',
    name: 'UserCenter',
    component: UserCenter,
    meta: { requiresAuth: true }
  },
  {
    path: '/messages',
    name: 'MessageBoard',
    component: MessageBoard
  },
  {
    path: '/admin',
    component: AdminLayout,
    meta: { requiresAuth: true, requiresAdmin: true },
    children: [
      {
        path: '',
        name: 'AdminDashboard',
        component: AdminDashboard,
        meta: { title: '管理仪表板' }
      },
      {
        path: 'users',
        name: 'AdminUsers',
        component: AdminUsers,
        meta: { title: '用户管理' }
      },
      {
        path: 'products',
        name: 'AdminProducts',
        component: AdminProducts,
        meta: { title: '商品管理' }
      },
      {
        path: 'orders',
        name: 'AdminOrders',
        component: AdminOrders,
        meta: { title: '订单管理' }
      },
      {
        path: 'messages',
        name: 'AdminMessages',
        component: AdminMessages,
        meta: { title: '留言管理' }
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('@/views/NotFound.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  // 设置页面标题
  if (to.meta.title) {
    document.title = `${to.meta.title} - 购物网站`
  } else {
    document.title = '购物网站'
  }

  // 已登录用户不能访问登录和注册页面
  if (to.meta.guest && userStore.isLoggedIn) {
    next({ name: 'Home' })
    return
  }

  // 需要登录的页面
  if (to.meta.requiresAuth && !userStore.isLoggedIn) {
    // 保存目标路由，登录后跳转
    next({ 
      name: 'Login', 
      query: { redirect: to.fullPath },
      replace: true
    })
    return
  }

  // 需要管理员权限的页面
  if (to.meta.requiresAdmin) {
    if (!userStore.isLoggedIn) {
      next({ 
        name: 'Login', 
        query: { redirect: to.fullPath },
        replace: true
      })
      return
    }
    
    if (!userStore.isAdmin) {
      // 不是管理员，重定向到首页
      next({ name: 'Home', replace: true })
      return
    }
  }

  next()
})

// 路由后置守卫
router.afterEach((to, from) => {
  // 滚动到顶部
  window.scrollTo(0, 0)
})

export default router 